package com.letv.core.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.letv.android.sdk.R;

/**
 * 自定义对话框
 * 
 * @author jiancong
 *
 */
public class LetvCommonDialog extends Dialog implements View.OnClickListener {
	/**
	 * 左按钮
	 */
	public static final int BUTTON_LEFT = 10;
	/**
	 * 右按钮
	 */
	public static final int BUTTON_RIGHT = 11;
	/**
	 * 中间按钮
	 */
	public static final int BUTTON_CENTER = 12;
	/**
	 * 默认布局
	 */
	private static final int DEFAULT_LAYOUT = R.layout.letv_layout_common_dialog;

	private Context mContext;
	private int mLayout = DEFAULT_LAYOUT;
	private View mRootView, mTwoBtnLayout;
	private TextView titleView, messageView;
	// 最多支持3个按钮
	private TextView leftButton, rightButton, centerButton;
	private DialogInterface.OnClickListener mLeftListener, mRightListener,
			mCenterListener;
	// 是否只有一个居中button
	private boolean hasOneBtn = false;

	public LetvCommonDialog(Context context) {
		super(context, R.style.upgrade_dialog_style);
		mContext = context;
		initView();
	}

	private void initView() {
		mRootView = LayoutInflater.from(mContext).inflate(mLayout, null);
		if (mRootView != null) {
			leftButton = (TextView) mRootView
					.findViewById(R.id.common_dialog_btn_left);
			rightButton = (TextView) mRootView
					.findViewById(R.id.common_dialog_btn_right);
			centerButton = (TextView) mRootView
					.findViewById(R.id.common_dialog_btn_center);
			titleView = (TextView) mRootView
					.findViewById(R.id.common_dialog_title);
			messageView = (TextView) mRootView
					.findViewById(R.id.common_dialog_content);
			mTwoBtnLayout = mRootView
					.findViewById(R.id.common_dialog_btn_layout);
			setContentView(mRootView);

			leftButton.setOnClickListener(this);
			rightButton.setOnClickListener(this);
			centerButton.setOnClickListener(this);
		} else {
			setContentView(new View(mContext));
		}
		setCanceledOnTouchOutside(false);
	}

	/**
	 * 设置对话框的属性
	 * 
	 * @param w
	 * @param h
	 * @param gravity
	 */
	public void setWindowParams(int w, int h, int gravity) {
		WindowManager.LayoutParams params = getWindow().getAttributes();
		params.width = w;
		params.height = h;
		params.gravity = gravity != -1 ? gravity : Gravity.CENTER;
		getWindow().setAttributes(params);
	}

	@Override
	public void setTitle(CharSequence title) {
		if (titleView != null && !TextUtils.isEmpty(title)) {
			titleView.setVisibility(View.VISIBLE);
			titleView.setText(title);
		}
	}

	@Override
	public void setTitle(int titleId) {
		setTitle(mContext.getText(titleId));
	}

	/**
	 * 设置对话框内容
	 * 
	 * @param content
	 */
	public void setContent(String content) {
		if (messageView != null && !TextUtils.isEmpty(content)) {
			messageView.setVisibility(View.VISIBLE);
			messageView.setText(content);
		}
	}

	/**
	 * 设置双按钮文字
	 * 
	 * @param leftText
	 * @param rightText
	 */
	public void setButtonText(int leftTextId, int rightTextId) {
		setButtonText(leftTextId == -1 ? null : mContext.getText(leftTextId),
				rightTextId == -1 ? null : mContext.getText(rightTextId));
	}

	/**
	 * 设置双按钮文字,默认左右按钮文字依次为确定、取消
	 * 
	 * @param leftText
	 * @param rightText
	 */
	public void setButtonText(CharSequence leftText, CharSequence rightText) {
		if (mRootView == null)
			return;
		hasOneBtn = false;
		if (!TextUtils.isEmpty(leftText)) {
			leftButton.setText(leftText);
		}
		if (!TextUtils.isEmpty(rightText)) {
			rightButton.setText(rightText);
		}
	}

	/**
	 * 设置单button的文字
	 * 
	 * @param centerTextId
	 */
	public void setButtonText(int centerTextId) {
		setButtonText(centerTextId == -1 ? null : mContext
				.getString(centerTextId));
	}

	/**
	 * 设置单button的文字
	 * 
	 * @param centerText
	 */
	public void setButtonText(CharSequence centerText) {
		if (centerButton != null && !TextUtils.isEmpty(centerText)) {
			hasOneBtn = true;
			centerButton.setText(centerText);
		}
	}

	/**
	 * 设置左文字点击事件
	 * 
	 * @param listener
	 */
	public void setLeftOnClickListener(DialogInterface.OnClickListener listener) {
		if (leftButton != null && leftButton.getVisibility() == View.VISIBLE)
			mLeftListener = listener;
	}

	/**
	 * 设置右文字点击事件
	 * 
	 * @param listener
	 */
	public void setRightOnClickListener(DialogInterface.OnClickListener listener) {
		if (rightButton != null && rightButton.getVisibility() == View.VISIBLE)
			mRightListener = listener;
	}

	/**
	 * 设置中文字点击事件
	 * 
	 * @param listener
	 */
	public void setCenterOnClickListener(
			DialogInterface.OnClickListener listener) {
		if (centerButton != null
				&& centerButton.getVisibility() == View.VISIBLE)
			mLeftListener = listener;
	}

	/**
	 * 取得布局根View
	 * 
	 * @return
	 */
	public View getRootView() {
		return mRootView;
	}

	@Override
	public void dismiss() {
		super.dismiss();
		// 清空数据
		titleView.setText("");
		messageView.setText("");
		messageView.setVisibility(View.GONE);
		hasOneBtn = false;
		setButtonText("", "");
		setButtonText("");
	}

	/**
	 * 设置只显示一个居中按钮
	 * 
	 * @param hasOneBtn
	 */
	public void setHasOneBtn(boolean hasOneBtn) {
		this.hasOneBtn = hasOneBtn;
	}

	@Override
	public void show() {
		if (mRootView != null) {
			if (hasOneBtn) {
				mTwoBtnLayout.setVisibility(View.GONE);
				centerButton.setVisibility(View.VISIBLE);
			} else {
				mTwoBtnLayout.setVisibility(View.VISIBLE);
				centerButton.setVisibility(View.GONE);
			}
		}
		super.show();
	}

	@Override
	public void onClick(View v) {
		if (R.id.common_dialog_btn_left == v.getId()){
			if (mLeftListener != null)
				mLeftListener.onClick(this, BUTTON_LEFT);
			else{
				dismiss();
			}
		}else if (R.id.common_dialog_btn_right == v.getId()){
			if (mRightListener != null){
				mRightListener.onClick(this, BUTTON_RIGHT);
			}
		}else if (R.id.common_dialog_btn_center == v.getId()){
			if (mCenterListener != null)
				mCenterListener.onClick(this, BUTTON_CENTER);
			else{
				dismiss();
			}
		}
	}
}
